<template>
  <div class="music-list">
    <div class="back" @click="goBack">
      <li class="icon-back"></li>
    </div>
    <h1 class="title">{{title}}</h1>
    <div class="bg-image" :style="bgImageStyle" ref="bgImage">
       <div
        class="play-btn-wrapper"
        :style="playBtnStyle"
      >
        <div
          v-show="songs.length > 0"
          class="play-btn"
          @click="random"
        >
          <i class="icon-play"></i>
          <span class="text">随机播放全部</span>
        </div>
      </div>
      <div class="filter" :style="filterStyle"></div>
    </div>

    <scroll  class="list"  :style="scrollStyle" v-loading="loading" @scroll="onScroll" :probe-type="3" v-result:[noResultText]="noResult">
      <!-- 歌手列表组件 -->
      <div class="song-list-wrapper">
        <song-list class="song-list-wrapper" @select="selectItem"  :songs='songs'>
      </song-list>
      </div>
    </scroll>
  </div>
</template>
<script>
import Scroll from '../base/scroll/scroll.vue'
import SongList from '../base/song-list/song-list'
import { mapActions } from 'vuex'
const RESERVED_HEIGHT = 40 // 歌曲距离顶部位置
export default {
  name: 'music-list',
  components: {
    Scroll,
    SongList
  },
  data() {
    return {
      imageHeight: 0, // 图片的高度
      scrollY: 0, // 滚动的实时位置
      maxTranslateY: 0 // 最大滚动的位置
    }
  },
  computed: { // 在计算属性中如果经常调用data数据的时候 需要用一个临时变量来本地缓存 优化vue技巧
    bgImageStyle() {
      const scrollY = this.scrollY
      let zIndex = 0
      let paddingTop = '70%'
      let height = 0
      let translateZ = 0
      if (scrollY > this.maxTranslateY) {
        paddingTop = 0
        height = `${RESERVED_HEIGHT}px`
        zIndex = 10 // 改变图层的等级
        translateZ = 1
      }
      let scale = 1
      if (scrollY < 0) {
        scale = 1 + Math.abs(scrollY / this.imageHeight)
      }
      return {
        zIndex,
        transform: `scale(${scale})translateZ(${translateZ}px)`,
        paddingTop,
        height,
        backgroundImage: `url(${this.pic})`
      }
    },
    scrollStyle() {
      return {
        top: `${this.imageHeight}px`
      }
    },
    filterStyle() {
      let blur = 0
      const scrollY = this.scrollY
      const imageHeight = this.imageHeight
      if (scrollY >= 0) {
        blur = Math.min(this.maxTranslateY / imageHeight, scrollY / imageHeight) * 20
      }
      return {
        backdropFilter: `blur(${blur}px)`
      }
    },
    noResult() {
      return !this.loading && !this.songs.length
    },
    playBtnStyle() {
      let display = ''
      if (this.scrollY >= this.maxTranslateY) { // 到达顶部 隐藏随机播放按钮
        display = 'none'
      }
      return {
        display
      }
    }
  },
  methods: {
    goBack() {
      this.$router.back()
    },
    onScroll(pos) {
      this.scrollY = -pos.y // 获取到实时滚动的位置
    },
    selectItem({ song, index }) { // 提交action 存储到vuex中
      this.selectPlay({
        list: this.songs,
        index
      })
    },
    random() { // 随机播放
      this.randomPlay(this.songs)
    },
    ...mapActions([ // 语法糖解构 拿到actions中的函数
      'selectPlay',
      'randomPlay'
    ])
  },
  mounted() {
      this.imageHeight = this.$refs.bgImage.clientHeight // 获取到图片的高度
      this.maxTranslateY = this.imageHeight - RESERVED_HEIGHT
  },
  props: {
     songs: { // 歌曲列表
        type: Array,
        default() {
          return []
        }
      },
      title: String,
      pic: String,
      loading: Boolean,
      noResultText: {
        type: String,
        default: '抱歉，没有找到歌曲!'
      }
  }
}
</script>

<style lang="scss" scoped>
  .music-list {
    position: relative;
    height: 100%;
    .back {
      position: absolute;
      top: 0;
      left: 6px;
      z-index: 20;
      transform: translateZ(2px);
      .icon-back {
        display: block;
        padding: 10px;
        font-size: $font-size-large-x;
        color: $color-theme;
      }
    }
    .title {
      position: absolute;
      top: 0;
      left: 10%;
      width: 80%;
      z-index: 20;
      transform: translateZ(2px);
      @include no-wrap();
      text-align: center;
      line-height: 40px;
      font-size: $font-size-large;
      color: $color-text;
    }
    .bg-image {
      position: relative;
      width: 100%;
      transform-origin: top;
      background-size: cover;
      .play-btn-wrapper {
        position: absolute;
        bottom: 20px;
        z-index: 10;
        width: 100%;
        .play-btn {
          box-sizing: border-box;
          width: 135px;
          padding: 7px 0;
          margin: 0 auto;
          text-align: center;
          border: 1px solid $color-theme;
          color: $color-theme;
          border-radius: 100px;
          font-size: 0;
        }
        .icon-play {
          display: inline-block;
          vertical-align: middle;
          margin-right: 6px;
          font-size: $font-size-medium-x;
        }
        .text {
          display: inline-block;
          vertical-align: middle;
          font-size: $font-size-small;
        }
      }
      .filter {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        background: rgba(7, 17, 27, 0.4);
      }
    }
    .list {
      position: absolute;
      bottom: 0;
      width: 100%;
      z-index: 0;
      .song-list-wrapper {
        padding: 20px 30px;
        background: $color-background;
      }
    }
  }
</style>
